//
//  BVRegisViewController.m
//  XiangYu
//
//  Created by YYKJ on 2017/6/5.
//  Copyright © 2017年 YYKJ. All rights reserved.
//

#import "BVRegisViewController.h"
#import "BVRegisNextViewController.h"
#import "BVLoginViewController.h"
#import "UIView+TransitionAnimation.h"
#import "ZZTextField.h"
#import "BVMineTimeAlertView.h"
#import "BVAddressAlertView.h"

@interface BVRegisViewController ()<UITextFieldDelegate>
{
    NSString *_selecedSex;
    NSString *_seleceDmarital;
}
/***   出生日期*/
@property (nonatomic ,strong) UITextField   *birthDateTF;
/***   工作地区*/
@property (nonatomic ,strong) UITextField   *addressTF;
/***   性别男*/
@property (nonatomic ,strong) UIButton      *manBtn;
/***   性别女*/
@property (nonatomic ,strong) UIButton      *womanBtn;
/***   未婚*/
@property (nonatomic ,strong) UIButton      *unmarriedBtn;
/***   离异*/
@property (nonatomic ,strong) UIButton      *divorceBtn;
/***   丧偶*/
@property (nonatomic ,strong) UIButton      *spouseBtn;

@end

@implementation BVRegisViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"注册"];
    [self layoutUI];
    
}

-(void)layoutUI{
    
    UIImageView *img = [[UIImageView alloc]init];
    [self.view addSubview:img];
    [img setImage:[UIImage imageNamed:@"login_image"]];
    img.sd_layout
    .topSpaceToView(self.view, 0)
    .centerXIs(ScreenW/2)
    .heightIs(160)
    .widthIs(320);
    
    UILabel *promptLb = [self labelWithTitle:@"30秒完成注册, 幸福一辈子"];
    [promptLb setTextColor:[UIColor BVTextGrayColor]];
    [promptLb setTextAlignment:NSTextAlignmentCenter];
    promptLb.sd_layout
    .topSpaceToView(img, 0)
    .centerXIs(ScreenW/2)
    .heightIs(30)
    .widthIs(300);
    
    UIView *line= [[UIView alloc]init];
    [self.view addSubview:line];
    [line setBackgroundColor:[UIColor BVLineGrayColor]];
    line.sd_layout
    .topSpaceToView(promptLb, 0)
    .centerXIs(ScreenW/2)
    .heightIs(1)
    .widthIs(300);
    
    UILabel *sexLb = [self labelWithTitle:@"我的性别:"];
    sexLb.sd_layout
    .topSpaceToView(line, 10)
    .leftSpaceToView(self.view, 10)
    .heightIs(40)
    .widthIs(80);
    
    self.manBtn = [self buttonWithTitle:@"男" withTag:100 ImageName:@"huiduihao" selectedImageName:@"hongduihao"];
    [self.manBtn addTarget:self action:@selector(sexBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.manBtn setSelected:YES];
    _selecedSex = @"男";
    self.manBtn.sd_layout
    .topSpaceToView(line, 10)
    .leftSpaceToView(sexLb, 10)
    .heightIs(40)
    .widthIs(60);
    
    self.womanBtn = [self buttonWithTitle:@"女" withTag:200 ImageName:@"huiduihao" selectedImageName:@"hongduihao"];
    [self.womanBtn addTarget:self action:@selector(sexBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.womanBtn.sd_layout
    .topSpaceToView(line, 10)
    .leftSpaceToView(self.manBtn, 0)
    .heightIs(40)
    .widthIs(60);
    
    UILabel *bornDataLb = [self labelWithTitle:@"出生日期:"];
    bornDataLb.sd_layout
    .topSpaceToView(sexLb, 20)
    .leftSpaceToView(self.view, 10)
    .heightIs(40)
    .widthIs(80);
    
    self.birthDateTF = [self textFieldWithPlaceholder:@"年 / 月 / 日"];
    [self.birthDateTF setTag:100];
    self.birthDateTF.sd_layout
    .topSpaceToView(sexLb, 20)
    .leftSpaceToView(bornDataLb, 10)
    .rightSpaceToView(self.view, 10)
    .heightIs(40);
    
    UILabel *workplaceLb = [self labelWithTitle:@"工作地区:"];
    workplaceLb.sd_layout
    .topSpaceToView(bornDataLb, 20)
    .leftSpaceToView(self.view, 10)
    .heightIs(40)
    .widthIs(80);
    
    self.addressTF = [self textFieldWithPlaceholder:@" 市 / 区"];
    [self.addressTF setTag:200];
    self.addressTF.sd_layout
    .topSpaceToView(self.birthDateTF, 20)
    .leftSpaceToView(workplaceLb, 10)
    .rightSpaceToView(self.view, 10)
    .heightIs(40);
    
    UILabel *maritalStatusLb = [self labelWithTitle:@"婚姻状况:"];
    maritalStatusLb.sd_layout
    .topSpaceToView(workplaceLb, 20)
    .leftSpaceToView(self.view, 10)
    .heightIs(40)
    .widthIs(80);
    
    self.unmarriedBtn = [self buttonWithTitle:@"未婚" withTag:300 ImageName:@"huiduihao" selectedImageName:@"hongduihao"];
    [self.unmarriedBtn addTarget:self action:@selector(maritalStatusBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.unmarriedBtn setSelected:YES];
    _seleceDmarital = @"未婚";
    self.unmarriedBtn.sd_layout
    .topSpaceToView(self.addressTF, 20)
    .leftSpaceToView(maritalStatusLb, 10)
    .heightIs(40)
    .widthIs(60);
    
    self.divorceBtn = [self buttonWithTitle:@"离异" withTag:400 ImageName:@"huiduihao" selectedImageName:@"hongduihao"];
    [self.divorceBtn addTarget:self action:@selector(maritalStatusBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.divorceBtn.sd_layout
    .topSpaceToView(self.addressTF, 20)
    .leftSpaceToView(self.unmarriedBtn, 10)
    .heightIs(40)
    .widthIs(60);
    
    self.spouseBtn = [self buttonWithTitle:@"丧偶" withTag:500 ImageName:@"huiduihao" selectedImageName:@"hongduihao"];
    [self.spouseBtn addTarget:self action:@selector(maritalStatusBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.spouseBtn.sd_layout
    .topSpaceToView(self.addressTF, 20)
    .leftSpaceToView(self.divorceBtn, 10)
    .heightIs(40)
    .widthIs(60);
    
    UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:nextBtn];
    [nextBtn setBackgroundColor:[UIColor BVBaseOrangeColor]];
    [nextBtn addTarget:self action:@selector(nextBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [UIView makeCorner:5.0 view:nextBtn];
    nextBtn.sd_layout
    .topSpaceToView(maritalStatusLb, 20)
    .leftSpaceToView(self.view, 20)
    .rightSpaceToView(self.view, 20)
    .heightIs(40);
    
}

-(void)nextBtnClick:(UIButton *)btn{
    
    if (self.birthDateTF.text.length <= 0) {
        [self showWarning:@"请输入正确出生日期"];
    }else{
        if (self.addressTF.text.length <= 0) {
            [self showWarning:@"请输入正确工作地址"];
        }else{
            BVRegisNextViewController *regisNext = [[BVRegisNextViewController alloc]init];
            regisNext.sex = [_selecedSex mutableCopy];
            regisNext.birth = [self.birthDateTF.text mutableCopy];
            regisNext.address = [self.addressTF.text mutableCopy];
            regisNext.marital = @"未婚";
            ROOTNAVPUSH(regisNext, YES);
        }
    }
}

-(void)sexBtnClick:(UIButton *)btn{
    
    if (btn.tag == 100) {
        _selecedSex = @"男";
        [self.manBtn setSelected:YES];
        [self.womanBtn setSelected:NO];
    }else{
        _selecedSex = @"女";
        [self.womanBtn setSelected:YES];
        [self.manBtn setSelected:NO];
    }
}

-(void)maritalStatusBtnClick:(UIButton *)btn{
    
    btn.selected == NO ? [btn setSelected:YES] : [btn setSelected:NO];
    
    if (btn.tag == 300) {
        _seleceDmarital = @"未婚";
        [self.divorceBtn setSelected:NO];
        [self.spouseBtn setSelected:NO];
    }else if (btn.tag == 400){
        _seleceDmarital = @"离异";
        [self.unmarriedBtn setSelected:NO];
        [self.spouseBtn setSelected:NO];
    }else{
        _seleceDmarital = @"丧偶";
        [self.unmarriedBtn setSelected:NO];
        [self.divorceBtn setSelected:NO];
    }
}

-(BOOL)textFieldShouldBeginEditing:(UITextField*)textField{
    [textField resignFirstResponder];
    
    if (textField.tag == 100) {
        BVMineTimeAlertView *alert = [[BVMineTimeAlertView alloc]init];
        [alert showMKPAlertView];
        alert.AlertResult = ^(NSString *str) {
            [textField setText:[str mutableCopy]];
        };
    }else if (textField.tag == 200){
        BVAddressAlertView *address = [[BVAddressAlertView alloc]init];
        address.AlertResult = ^(NSString *str) {
            [textField setText:[str mutableCopy]];
        };
        [address showMKPAlertView];
    }
    return NO;
}

-(UITextField *)textFieldWithPlaceholder:(NSString *)placeholder{
    
    UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 10, 45)];
    [imgView setImage:[UIImage imageNamed:@"toiuming"]];
    
    UITextField *textField = [[UITextField alloc]init];
    [self.view addSubview:textField];
    [textField.layer setBorderColor:[UIColor BVBaseOrangeColor].CGColor];
    [textField.layer setBorderWidth:1.0];
    [textField .layer setCornerRadius:5.0];
    [textField setPlaceholder:placeholder];
    [textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
    [textField setTextColor:[UIColor BVTextDarkGrayColor]];
    [textField setFont:[UIFont systemFontOfSize:16]];
    [textField setLeftView:imgView];
    [textField setLeftViewMode:UITextFieldViewModeAlways];
    [textField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
    [textField setDelegate:self];
    
    return textField;
}

-(UILabel *)labelWithTitle:(NSString *)title{
    
    UILabel *label = [[UILabel alloc]init];
    [self.view addSubview:label];
    [label setTextAlignment:NSTextAlignmentRight];
    [label setText:title];
    [label setFont:[UIFont systemFontOfSize:16.0]];
    [label setTextColor:[UIColor BVTextDarkGrayColor]];
    return label;
}

-(UIButton *)buttonWithTitle:(NSString *)title withTag:(NSInteger )tag ImageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName{
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:btn];
    [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:selectedImageName] forState:UIControlStateSelected];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [btn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 20)];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor BVTextGrayColor] forState:UIControlStateNormal];
    [btn setTag:tag];
    
    return btn;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}


@end
